<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class attributes_to_object extends CI_Controller {

        function __construct()
        {
            parent::__construct();
            $this->load->model('attributes_to_object_model','atb_model');
        }

        public function index(){show_404();}

        public function view_new($object_TYPE,$group_ID)
        {
            $this->data = array(
                'title' => "Add attribute to object",
                'type' => $object_TYPE,
                'attributes' => $this->atb_model->get_all_custom_attributes($group_ID),
                'group_ID' => $group_ID
            );
            echo $this->load->view('attributes_to_object/new_object_attribute',$this->data,TRUE); 
        }

        function view_edit($object_TYPE,$group_ID){

            $this->data = array(
                'title' => "Edit attributes",
                'type' => $object_TYPE,
                'object_attributes' => $this->atb_model->get_object_attributes($object_TYPE,$group_ID)
            );
            echo $this->load->view('attributes_to_object/edit_object_attributes',$this->data,TRUE);            
        }

        function submit_post()
        {

            if ($this->form_validation->run('object_attributes') == false)
            {
                echo json_encode(array('action'=>FALSE, 'msg'=>validation_errors()));
            } 
            else
            {                            

                $record['type'] = $_POST['attrToObjectDialog_type'];
                $record['custom'] = $_POST['attrToObjectDialog_custom'];
                $record['attributes_ID'] = $_POST['attrToObjectDialog_attributes_ID'];
                $record['groups_ID'] = $_POST['attrToObjectDialog_group_ID'];

                if ($this->atb_model->insert($record) == TRUE)
                {
                    echo json_encode(array('action'=>true,'msg'=>'Successful insert operation.'));
                }
                else
                {
                    echo json_encode(array('action'=>FALSE, 'msg'=>'<p>An Error Occured.</p>')); 

                }
            }

        }

        function delete($ID){
            $this->db->where('ID',$ID);
            $this->db->delete('object_attributes');
            echo json_encode(array('action'=>true));

        }

        function reorder_object_attributes()
        {
            $record['type'] = $_POST['editAttrToObjectDialog_type'];
            unset($_POST['editAttrToObjectDialog_type']);

            $cnt = 0;
            foreach ($_POST as $value):
                $id = explode('_',$value);
                $this->db->where('ID',$id[1]);
                $this->db->update('object_attributes',array('order'=>$cnt));
                $cnt++;
                endforeach;
        }

        function dropdown_check($str)
        {
            if ($str == '0')
            {
                $this->form_validation->set_message('dropdown_check', 'Please choose object <b>%s</b>!');
                return FALSE;
            }
            else
            {
                return TRUE;
            }
        }

        public function set_default()
        {
            $type = $_POST['object_type'];
            $groups_ID = $_POST['group_ID'];
            $custom = 0;

            $attribute_length = count($_POST['attributes']);

            // DELETE OBJECT GROUP FIRST
            $q = "DELETE FROM object_attributes WHERE type = '$type' AND groups_ID = $groups_ID";
            $this->db->query($q);

            for($i=0;$i<$attribute_length;$i++){
                $data = array(
                    'type'=>$type,
                    'groups_ID'=>$groups_ID,
                    'custom'=>$custom,
                    'attributes_ID'=>$_POST['attributes'][$i],
                    'order'=>$i,
                );
                //$this->firephp->log($data);
                $this->db->insert('object_attributes',$data);
            }

            redirect(base_url("attributes_to_object/view_all?object_type=$type&group_ID=$groups_ID"));
        }

        function view_all()
        {
            $this->load->model('attributes_model');

            $this->data = array(
                'title' => "View attributes",
                'attributes' => $this->attributes_model->view_all_attributes(),
                'object_attributes' => $this->atb_model->get_object_attributes($_GET['object_type'],$_GET['group_ID'],TRUE)
            );
            $this->firephp->log($this->data);
            $this->template->build('attributes_to_object/view_all_attributes',$this->data);
        }
}